﻿using System;

using MSAP.ObjectsStore;

namespace TestObjectsStore
{
    class Program
    {
        static PersonneSet MesContacts = new PersonneSet();

        static void Main(string[] args)
        {
            TestAvecSOAP();
            TestAvecJSON();
        }

        static void TestAvecSOAP()
        {
            CreateSample();
            Console.WriteLine("SOAP : MesContact.Count = " + MesContacts.Records.Count);

            Console.WriteLine("MesContact.Save");
            MesContacts.Save("TestFile.soap.xml");
            MesContacts = null;
            Console.WriteLine("MesContact null");

            MesContacts = new PersonneSet();
            Console.WriteLine("MesContact.Load");
            MesContacts.Load("TestFile.soap.xml");
            Console.WriteLine("MesContact.Count = " + MesContacts.Records.Count);

            Console.WriteLine("Appuyez sur une touche pour arreter");
            Console.ReadKey();
        }

        static void TestAvecJSON()
        {
            Console.WriteLine("JSON : MesContact.Count = " + MesContacts.Records.Count);

            PersonneSet.RecordSetProvider = JSONRecordSetProvider<PersonneRecord>.CreateRecordSetProvider();

            Console.WriteLine("MesContact.Save");
            MesContacts.Save("TestFile.json");
            MesContacts = null;
            Console.WriteLine("MesContact null");

            MesContacts = new PersonneSet();
            Console.WriteLine("MesContact.Load");
            MesContacts.Load("TestFile.json");
            Dump(MesContacts);
            Console.WriteLine("MesContact.Count = " + MesContacts.Records.Count);

            Console.WriteLine("Appuyez sur une touche pour arreter");
            Console.ReadKey();
        }

        static void Dump(PersonneSet personnes)
        {
            Console.WriteLine("*** DUMP ***");
            Console.WriteLine("personnes.Count = " + personnes.Records.Count);
            foreach (PersonneRecord rec in personnes.Records)
            {
                Console.WriteLine("oid = " + rec.Oid.ToString());
                Console.WriteLine("Nom = " + rec.Nom);
            }
        }

        static void CreateSample()
        {
            Console.WriteLine("Begin CreateSample()");
            for (int i = 1; i < 30; i++)
            {
                PersonneRecord contact = MesContacts.CreateRecord();
                contact.Nom = "Nom " + i.ToString();
                contact.Prenom = "Prenom " + i.ToString();
                int ii=i;
                contact.Telephones.Add(new Telephone(ii.ToString(), ii.ToString(), ii.ToString()));
                ii++;
                contact.Telephones.Add(new Telephone(ii.ToString(), ii.ToString(), ii.ToString()));
                ii++;
                contact.Telephones.Add(new Telephone(ii.ToString(), ii.ToString(), ii.ToString()));
            }
            Console.WriteLine("End CreateSample()");
        }
    }
}
